//
//  DVBaseMacro.h
//  DVBaseTool
//
//  Created by 松鼠君 on 2019/3/5.
//  Copyright © 2019 David. All rights reserved.
//

#ifndef DVBaseMacro_h
#define DVBaseMacro_h

typedef void(^ItemHandler)(NSInteger index);

#pragma mark - Comm
/**  Comm */
#define kDoBlock(X) !X?:X();
// 定义软引用
#define _WeakSelf \
__weak __typeof__ (self) self_weak_ = self;
#define _StrongSelf \
__strong __typeof__(self) self = self_weak_;

#pragma mark - Method
//打印所有方法
#define CPrintAllMethodList(Class) \
unsigned int count;\
Method *methods = class_copyMethodList([Class class], &count);\
for (int i = 0; i < count; i++) {\
Method method = methods[i];\
SEL selector = method_getName(method);\
NSString *name = NSStringFromSelector(selector);\
NSLog(@"method_getName:%@",name);\
}\

/**  单例宏 */
// .h文件
#define JYSingletonH + (instancetype)sharedInstance;
// .m文件
#define JYSingletonM \
\
static id _instance; \
+ (instancetype)allocWithZone:(struct _NSZone *)zone {\
\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
\
_instance = [super allocWithZone:zone];\
});\
return _instance;\
}\
\
+ (instancetype)sharedInstance {\
\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [[self alloc] init];\
});\
return _instance;\
}\
\
- (id)copyWithZone:(NSZone *)zone {\
\
return _instance;\
}

/**  单例宏-传方法名字 */
// .h文件
#define JYSingletonNH(name) + (instancetype)shared##name;
// .m文件
#define JYSingletonNM(name) \
static id _instance; \
\
+ (instancetype)allocWithZone:(struct _NSZone *)zone \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [super allocWithZone:zone]; \
}); \
return _instance; \
} \
\
+ (instancetype)shared##name \
{ \
static dispatch_once_t onceToken; \
dispatch_once(&onceToken, ^{ \
_instance = [[self alloc] init]; \
}); \
return _instance; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return _instance; \
}
/**
 *  判断一个对象是否为空（包括空格）
 *
 *  @param thing 对象
 *
 *  @return 返回结果
 */
static inline BOOL ICIsObjectEmpty(id thing){
    return thing == nil ||
    ([thing isEqual:[NSNull null]]) ||([thing isEqualToString:@" "])||
    ([thing respondsToSelector:@selector(length)] && [(NSData *)thing length] == 0) ||
    ([thing respondsToSelector:@selector(count)]  && [(NSArray *)thing count] == 0);
}




#pragma mark - Debug
///------ 替换NSLog使用，debug模式下可以打印很多方法名、行信息(方便查找)，release下不会打印 ------
#ifdef DEBUG

/**
 *  在Debug模式下，输出内容
 */
#define DLOG(...)   NSLog(__VA_ARGS__)

/**
 *  在Debug模式下，输出Point信息
 *
 *  @param p 点
 *
 *  @return 点的x和y
 */
#define DLOGPOINT(p)    NSLog(@"%f,%f", p.x, p.y);

/**
 *  在Debug模式下，输出Size信息
 *
 *  @param size 大小
 *
 *  @return 大小的width和height
 */
#define DLOGSIZE(size)    NSLog(@"%f,%f", size.width, size.height);

/**
 *  在Debug模式下，输出Fame信息
 *
 *  @param  p  位置信息
 *
 *  @return 位置的x、y、width和height
 */
#define DLOGRECT(p)    NSLog(@"%f,%f %f,%f", p.origin.x, p.origin.y, p.size.width, p.size.height);

#else

#define DLOG(...)
#define DLOGPOINT(p)
#define DLOGSIZE(p)
#define DLOGRECT(p)

#endif



#pragma mark - String
/**  String */
#define str(...) [NSString stringWithFormat:__VA_ARGS__]
#define kStrIsExist(Str) (!ICIsStringEmpty(Str))
/**
 *  判断一个字符串是否为空
 *
 *  @param string 字符串
 *
 *  @return 返回结果
 */
static inline BOOL ICIsStringEmpty(NSString *string){
    if (string == nil) {
        return YES;
    }
    if([string respondsToSelector:@selector(length)]){
        if (string.length == 0) {
            return YES;
        }
    }else{
        return YES;
    }
    if ([string isEqualToString:@"<null>"]) {
        return YES;
    }
    if ([string isEqualToString:@"(null)"]) {
        return YES;
    }
    return NO;
}

/**
 过滤 value 中空值
 
 @param value   需要判断过滤的 value 值
 @return        处理后的结果
 */
static inline NSString *SafeValue(id value) {
    if (!value) {
        return @"";
    }
    else if ([value isKindOfClass:[NSString class]]) {
        if ([value isEqualToString:@"<null>"] || [value isEqualToString:@"null"] || [value isEqualToString:@"(null)"] || [value isEqualToString:@"nil"]) {
            return @"";
        }
        else {
            return value;
        }
        
    }
    else {
        return [NSString stringWithFormat:@"%@", value];
    }
}

#pragma mark - UI
/**  UI */
#ifndef kHexColor//(color)
#define kHexColor(color) [UIColor colorWithHexString:color]
#endif

#define kFont(num) [UIFont systemFontOfSize:num]
#define KWindow ([UIApplication sharedApplication].windows.firstObject)
 // 根据名字返回对应的图片
#define img(name) [UIImage imageNamed:name]
//是否为iPhoneX
#ifndef iPhoneX
#define iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)
#endif
//设备屏幕高度
#ifndef UIScreenHeight
#define UIScreenHeight   [UIScreen mainScreen].bounds.size.height
#endif
//设备屏幕宽度
#ifndef UIScreenWidth
#define UIScreenWidth    [UIScreen mainScreen].bounds.size.width
#endif
//FastGetter
#define kFastGetterView(name) \
-(UIView*)name{\
if(_##name){\
_##name = [UIView new];\
}\
return _##name;\
}
/**  适配函数 */
static inline UIEdgeInsets dv_safeAreaInset(UIView *view) {
    if (@available(iOS 11.0, *)) {
        return view.safeAreaInsets;
    }
    return UIEdgeInsetsZero;
}
/**  TableView */
//处理抖动
#define kTableViewReloadNoDangler(tableView)\
if (@available(iOS 11, *)) {\
tableView.estimatedRowHeight = 0;\
tableView.estimatedSectionHeaderHeight = 0;\
tableView.estimatedSectionFooterHeight = 0;\
}
/**  ScroolView */
#define  adjustsScrollViewInsets(scrollView)\
do {\
if (@available(iOS 11.0, *)) {\
scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;\
}else{\
\
if([self.class isKindOfClass:UIViewController.class]){\
self.automaticallyAdjustsScrollViewInsets = NO;\
}\
}\
} while (0);








#endif /* DVBaseMacro_h */
